#include <stdlib.h>

#include "hal_Delay.h"
#include "userHandler.h"
#include "bsp_spi.h"
#include "bsp_gpio.h"
#include "userProfileLut.h"
#include "userCfgDefault.h"
#include "util_factory.h"


RC8088_RWCfg_st pCfg[SPI_NUM_MAX];

//(uint8_t *)RC8088_REG_ADDR;
//(uint8_t *)RC8088_PROFILE_ADDR;
//(uint8_t *)RC8088_USERCFG_ADDR;
#if 0
void test_data(STRUCT_PROFILEMEM_ADDR *m_profMemAddr,uint8_t* buf)
{
//test1
#if 0
//	*(m_profMemAddr->profileAddr)     =0x12345678;                               					 //192*28=5376Byte
	*(uint32_t *)(m_profMemAddr->rampMemAddr)     =0x11112222;                     //240Byte
	*(uint32_t *)(m_profMemAddr->startFreqMemAddr)=0x22223333;    //80Byte
	*(uint32_t *)(m_profMemAddr->adcRmaMemAddr)   =0x33334444;//40Byte
	*(uint32_t *)(m_profMemAddr->txEnMemAddr)     =0x44445555;   //20Byte
	*(uint32_t *)(m_profMemAddr->bpmMemAddr)      =0x55556666;       //2048Byte
	*(uint32_t *)(m_profMemAddr->tx0PhaseMemAddr) =0x66667777;               //20Byte
	*(uint32_t *)(m_profMemAddr->tx1PhaseMemAddr) =0x77778888;   //20Byte
	*(uint32_t *)(m_profMemAddr->tx2PhaseMemAddr) =0x88889999;   //20Byte
	*(uint32_t *)(m_profMemAddr->tx3PhaseMemAddr) =0x9999aaaa;   //20Byte
	*(uint32_t *)(m_profMemAddr->tx4PhaseMemAddr) =0xaaaabbbb;   //20Byte
	*(uint32_t *)(m_profMemAddr->tx5PhaseMemAddr) =0xbbbbcccc;   //20Byte
	*(uint32_t *)(m_profMemAddr->tx6PhaseMemAddr) =0xccccdddd;   //20Byte
	*(uint32_t *)(m_profMemAddr->tx7PhaseMemAddr) =0xddddeeee;   //20Byte
 	*(uint32_t *)(m_profMemAddr->testMemAddr)     =0xeeeeffff; 

	//buf printf
	printf("**********\n");
	for(int i=0;i<5;i++){
		printf("%.2x ",buf[i]);
	}
	printf("\n");
	for(int i=0;i<5;i++){
		printf("%.2x ",buf[i+5376]);
	}
	printf("\n");
	for(int i=0;i<5;i++){
		printf("%.2x ",buf[i+5376+240]);
	}
	printf("\n");
	for(int i=0;i<5;i++){
		printf("%.2x ",buf[i+5376+240+80]);
	}
	printf("\n");
	for(int i=0;i<5;i++){
		printf("%.2x ",buf[i+5376+240+80+40]);
	}
	printf("\n");
	for(int i=0;i<5;i++){
		printf("%.2x ",buf[i+5376+240+80+40+20]);
	}
	printf("\n");
	for(int i=0;i<5;i++){
		printf("%.2x ",buf[i+5376+240+80+40+20+2048]);
	}
	printf("\n");
	for(int j=0;j<8;j++){
		for(int i=0;i<5;i++){
			printf("%.2x ",buf[i+5376+240+80+40+20+2048+j*20]);
		}
		printf("\n");
	}
	printf("\n**********\n");
#else
	for(int i=0;i<8192;i++)
	{
		buf[i] = (uint8_t)i;
	}
	printf("**********\n");
	printf("0x%.8x :0x%.8x\n",m_profMemAddr->profileAddr,*(uint32_t *)(m_profMemAddr->profileAddr));
	printf("%x, %x\n", m_profMemAddr->rampMemAddr, *(uint32_t *)(m_profMemAddr->rampMemAddr));
	printf("%x, %x\n", m_profMemAddr->startFreqMemAddr, *(uint32_t *)(m_profMemAddr->startFreqMemAddr));
	printf("%x, %x\n", m_profMemAddr->adcRmaMemAddr, *(uint32_t *)(m_profMemAddr->adcRmaMemAddr));
	printf("%x, %x\n", m_profMemAddr->txEnMemAddr, *(uint32_t *)(m_profMemAddr->txEnMemAddr));
	printf("%x, %x\n", m_profMemAddr->bpmMemAddr, *(uint32_t *)(m_profMemAddr->bpmMemAddr));
	printf("%x, %x\n", m_profMemAddr->tx0PhaseMemAddr, *(uint32_t *)(m_profMemAddr->tx0PhaseMemAddr));
	printf("%x, %x\n", m_profMemAddr->tx1PhaseMemAddr, *(uint32_t *)(m_profMemAddr->tx1PhaseMemAddr));
	printf("%x, %x\n", m_profMemAddr->tx2PhaseMemAddr, *(uint32_t *)(m_profMemAddr->tx2PhaseMemAddr));
	printf("%x, %x\n", m_profMemAddr->tx3PhaseMemAddr, *(uint32_t *)(m_profMemAddr->tx3PhaseMemAddr));
	printf("%x, %x\n", m_profMemAddr->tx4PhaseMemAddr, *(uint32_t *)(m_profMemAddr->tx4PhaseMemAddr));
	printf("%x, %x\n", m_profMemAddr->tx5PhaseMemAddr, *(uint32_t *)(m_profMemAddr->tx5PhaseMemAddr));
	printf("%x, %x\n", m_profMemAddr->tx6PhaseMemAddr, *(uint32_t *)(m_profMemAddr->tx6PhaseMemAddr));
	printf("%x, %x\n", m_profMemAddr->tx7PhaseMemAddr, *(uint32_t *)(m_profMemAddr->tx7PhaseMemAddr));
	printf("%x, %x\n", m_profMemAddr->testMemAddr, *(uint32_t *)(m_profMemAddr->testMemAddr));
	printf("\n**********\n");
#endif
}
#endif
uint32_t head[2]={0x04030201,0x08070605};
void WaitFPGA_MipiDone(RC8088_RWCfg_st *pCfg){
		// rc8088_regCfg->MIPI.MIPI_CFG0.BIT.mipi0_en=1;
		// rc8088_regCfg->MIPI.MIPI_CFG0.BIT.mipi1_en=1;
		// HAL_RC8088_WrReg(pCfg, REG_MIPI_CFG0, &rc8088_regCfg->MIPI.MIPI_CFG0, 1);
	RC8088_OperateStatus rtn;
	RC8088_RegDat_st u32Rdata;
	rtn=HAL_RC8088_RdReg(pCfg, REG_MIPI_CFG0,&u32Rdata, 1);
	u32Rdata.DATA32 = u32Rdata.DATA32 |(1<<20); //mipi0 en 
	u32Rdata.DATA32 = u32Rdata.DATA32 |(1<<21); //mipi1 en 
	HAL_RC8088_WrReg(pCfg, REG_MIPI_CFG0, &u32Rdata, 1);
}

void RC8088_userInit(RC8088_RWCfg_st *pCfg){
//	uint32_t memLenB128=0;
	uint32_t rtn=0;
	RC8088_RegDat_st u32Rdata;

	RC8088_RegCfg_st *rc8088_regCfg;//=(RC8088_RegCfg_st *)(RC8088_REG_ADDR);
	STRUCT_RC8088_USERCFG *rc8088_userCfg;//=(STRUCT_RC8088_USERCFG *)(RC8088_USERCFG_ADDR);
	STRUCT_PROFILEMEM_ADDR *profMemAddr;

//	(void *)aaa= malloc(sizeof(RC8088_RegCfg_st));
	rc8088_regCfg = malloc(sizeof(RC8088_RegCfg_st));
	rc8088_userCfg = malloc(sizeof(STRUCT_RC8088_USERCFG));
	profMemAddr = malloc(sizeof(STRUCT_PROFILEMEM_ADDR));
	// printf("rc8088_regCfg is %p.\n", (void *)rc8088_regCfg);
	// printf("rc8088_userCfg is %p.\n", (void *)rc8088_userCfg);
	// printf("profMemAddr is %p.\n", (void *)profMemAddr);

	memset(g_profile_mem,0,8192);
	memset(rc8088_regCfg,0,sizeof(RC8088_RegCfg_st));
	memset(profMemAddr,0,sizeof(STRUCT_PROFILEMEM_ADDR));  //需要清空addr里面的值，暂不知道意义，先不清
	memset(rc8088_userCfg,0,sizeof(STRUCT_RC8088_USERCFG));
	RC8088_ProfileMemAddrInit(profMemAddr);
//add by zzg test memaddr  20250115 15:04
//	test_data(profMemAddr,g_profile_mem);

	if (pCfg->SPI_RF_DEV == RC8088_RF_0)
	{
		RC8088_userCfg_far(rc8088_userCfg);
	}
	else if (pCfg->SPI_RF_DEV == RC8088_RF_1)
	{
		RC8088_userCfg_near(rc8088_userCfg);
	}
	else
	{
		printf("RC8088_userInit pCfg->SPI_RF_DEV err, SPI_RF_DEV is %u.\n", pCfg->SPI_RF_DEV);
		while(1);
	}
	RC8088_userCfgDefault(pCfg, rc8088_userCfg,profMemAddr);
	rc8088_userCfg->userMipi.mipi0_en=0;
	rc8088_userCfg->userMipi.mipi1_en=0;
	rtn=HAL_RC8088_RegLoadConfig(pCfg,rc8088_regCfg,profMemAddr,rc8088_userCfg);
	if(rtn){
		printf("HAL_RC8088_RegLoadConfig error=%d\n",rtn);
		while(1);
	}

	#if 1
	rtn=HAL_RC8088_ReadCfg(pCfg,rc8088_regCfg);
	if(rtn){
		printf("HAL_RC8088_ReadCfg error=%d\n",rtn);
		while(1);
	}
	#endif
	#if AUTO_WAVE_EN 
		RC8088_ProfileLutConfig(pCfg,rc8088_regCfg,profMemAddr,rc8088_userCfg);
	#endif
	#if TEST_COMPELX_WAVE_EN //complex wave
		RC8088_ProfileLutComplexWaveConfig();
	#endif
	#if TEST_TXPHASE_WAVE_EN
		RC8088_ProfileLutTXPhaseConfig();
	#endif
	#if TEST_CALIB_WAVE_EN
			RC8088_ProfileLutCalibWaveConfig();
	#endif
	rc8088_regCfg->OP_GATE.OP_GATE.WORD = 0;
	HAL_RC8088_WrReg(pCfg,REG_OP_GATE, &rc8088_regCfg->OP_GATE, 1);
	HAL_RC8088_ClrAllInt(pCfg,&rc8088_regCfg->INT);
	HAL_RC8088_ClrESR0Int(pCfg,&rc8088_regCfg->INT);
	Cnt_Delay_ms(1);
	rtn=HAL_RC8088_RdReg(pCfg, REG_ISR,&u32Rdata, 1);printf("before:isr=%08X\n",u32Rdata.DATA32);
	printf("HAL_RC8088_RdReg REG_ISR rtn is %u.\n", rtn);
	rtn=HAL_RC8088_RdReg(pCfg, REG_ESR0,&u32Rdata, 1);printf("before:esr0=%08X\n",u32Rdata.DATA32);
	printf("HAL_RC8088_RdReg REG_ESR0 rtn is %u.\n", rtn);

	free(rc8088_regCfg);
	free(rc8088_userCfg);
	free(profMemAddr);

	Cnt_Delay_ms(1);

}

void send_ad_data_to_mipi(RC8088_RWCfg_st *pCfg ,int frame_num)
{
	RC8088_RegDat_st u32Rdata;
	//	LED_WritePin = 1;
	for(uint32_t frameNow=0;frameNow<frame_num;frameNow++){
        #if TEST_AUTO_CALIB_EN
            RC8088_ProfileLutTXPhaseReload(frameNow);
//            if(frameNow%128==0){
//                RC8088_ProfileLutPASwitch(((frameNow>>7)&0xFF)+0);
//            }
        #endif
		#if TEST_TXPHASE_WAVE_EN
            #if TEST_TXPHASE_SHOW_EN
            #else
                RC8088_ProfileLutTXPhaseReload(frameNow);
                if(frameNow%128==0){
                    RC8088_ProfileLutPASwitch(((frameNow>>7)&0xFF)+0);
                }
            #endif

		#endif
		HAL_RC8088_WaveStart(pCfg);
		#if 0
			while(1){
					testVal=sCnt<<14;sCnt++;
					if(sCnt>7) sCnt=0;
					testBuf[4]=(testVal>>8)&0xFF;
					testBuf[5]=(testVal>>16)&0xFF;
					QSPI_TransmitTest(RC8088_QSPI,&testBuf[0],7);
	//            HAL_RC8088_RdReg(pCfg, &rc8088_regCfg->ANA.CFG02,&u32Rdata, 1);printf("ANA.CFG02=%08X\n",u32Rdata.DATA32);
	//            Cnt_Delay_ms(100);
			}
		#endif
//        while(1);
        while(1){
			thread_mSleep(150);break;
            #if 0
                HAL_RC8088_RdReg(pCfg, &rc8088_regCfg->INT.ISR,&u32Rdata, 1);
                rc8088_regCfg->INT.ISR.WORD=u32Rdata.DATA32;
                if(rc8088_regCfg->INT.ISR.BIT.prep_frame_end){
    //                printf("frameEnd=%08X\n",rc8088_regCfg->INT.ISR.WORD);
                    rc8088_regCfg->INT.ISR.WORD=0;
                    rc8088_regCfg->INT.ISR.BIT.prep_frame_end=1;
                    HAL_RC8088_WrReg(pCfg, &rc8088_regCfg->INT.ISR, 1);
                    break;
                }
            #endif
        }
		#if 0
//		HAL_RC8088_RdReg(pCfg, REG_ISR,&u32Rdata, 1);printf("ISR=%08X\n",u32Rdata.DATA32);
		HAL_RC8088_RdReg(pCfg, REG_ESR0,&u32Rdata, 1);printf("ESR0=%08X\n",u32Rdata.DATA32);
		if(u32Rdata.DATA32){
			printf("ESR0=%08X\n",u32Rdata.DATA32);
			while(1);
		}
		#endif
	}
//	LED_WritePin=0;
//    while(1);
	#if 0 //test pbuf0
		memLenB128=16*8*2/16;
		memset((uint8_t *)BB_DBUF0_BASE,0,memLenB128*16);
		for(uint32_t i=0; i<memLenB128*4; i++)
		{
				Write_M8(BB_DBUF0_BASE+i*4,i);
				Write_M8(BB_DBUF0_BASE+i*4+1,i);
				Write_M8(BB_DBUF0_BASE+i*4+2,i);
				Write_M8(BB_DBUF0_BASE+i*4+3,i);
		}	
		HAL_RC8088_WrMem(pCfg, RC8088_PBUFF0_BASE, (RC8088_MemDat_st *)BB_DBUF0_BASE, memLenB128);//len:1=16Byte
		HAL_RC8088_RdMem(pCfg, RC8088_PBUFF0_BASE, (RC8088_MemDat_st *)BB_DBUF0_BASE, memLenB128);
		HAL_RC8088_TrigEdma();
	#endif
	#if 0
		Cnt_Delay_ms(10);
		memset((uint8_t *)BB_DBUF0_BASE,0,65536);
		memLenB128=rc8088_userCfg->numPT*rc8088_userCfg->numRX*NUM_BYTE16>>4;
		HAL_RC8088_RdMem(pCfg, RC8088_PBUFF0_BASE, (RC8088_MemDat_st *)BB_DBUF0_BASE, memLenB128);
		UART_Transmit_Bytes(&UART0, (uint8_t*)&head[0], 8);
		UART_Transmit_Bytes(&UART0, (uint8_t *)BB_DBUF0_BASE,rc8088_userCfg->numPT*rc8088_userCfg->numRX*NUM_BYTE16);
	#endif
		// while(1){
		// 	// if(GPIO_GET_IN_BIT(MIPI_INIT_GPIO_Port,MIPI_INIT_Pin)==0){
		// 	// 	printf("MIPI send over\n");
		// 	// 	break;
		// 	// }
		// 	printf("1s wait\n");
		// 	Cnt_Delay_s(1);
		// }
	#if 0
		HAL_RC8088_RdReg(pCfg, &rc8088_regCfg->INT.ISR,&u32Rdata, 1);printf("ISR=%08X\n",u32Rdata.DATA32);
		HAL_RC8088_RdReg(pCfg, &rc8088_regCfg->INT.ESR0,&u32Rdata, 1);printf("ESR0=%08X\n",u32Rdata.DATA32);
	#endif
	#if 0 //test pbuf0
		HAL_RC8088_RdMem(pCfg, RC8088_PBUFF0_BASE, (RC8088_MemDat_st *)BB_DBUF0_BASE, memLenB128);
		printf("After\n");
		for(uint32_t i=0; i<memLenB128*16; i++)
		{
				uint32_t tmpVal=Read_M8(BB_DBUF0_BASE+i);
				printf("%02X ", tmpVal);
		}	
		printf("\n");
	#endif		
	#if 0 //test mipi init gpio 1.0
		while(1){
			if(GPIO_GET_IN_BIT(MIPI_INIT_GPIO_Port,MIPI_INIT_Pin)==0){
				printf("MIPI not done\n");
			}else{
				printf("MIPI init done\n");
			}
			Cnt_Delay_ms(1000);
		}
		
	#endif
	thread_mSleep(200);
//	WDT_Set_ms(0);
	HAL_RC8088_WaveStop(pCfg);
}

void RC8088_userCfgLoop(RC8088_RWCfg_st *pCfg)
{

}


/*****
 *   rf_index   0 ： 远端     2：近端
 * 
 * 
 */
 
int rf_skrl_init(uint8_t rf_index)
{
	uint32_t l_u32_i;

	chip_en_8088();
    usleep(1000);
	l_u32_i = 1;  //近端

	switch(rf_index){
		case 0: //远端
			pCfg[0].SPI_RF_DEV = RC8088_RF_0;
			RC8088_userInit(&pCfg[0]);
		break;
		case 2: //近端
			pCfg[1].SPI_RF_DEV = RC8088_RF_1;
    		RC8088_userInit(&pCfg[1]);
		break;
		default:
			printf("err rf_index_num:%d.\n",rf_index);
		break;
	}	

    usleep(1000);
	return 0;

}

int rf_fmcw_send(int dev,int frame_num)
{
    // //test
    WaitFPGA_MipiDone(&pCfg[dev]);
    send_ad_data_to_mipi(&pCfg[dev],frame_num);
}












